package com.jctrip.hotel.source.xxl.wehotel.thread;

import com.jctrip.hotel.common.constant.RedisKeyConstant;
import com.jctrip.hotel.common.context.BeanContext;
import com.jctrip.hotel.common.util.RedisUtil;
import com.jctrip.hotel.source.model.wehotel.resq.GetHotelInfo;
import com.jctrip.hotel.source.service.wehotel.WeHotelTaskService;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author henry
 * @Date 2023/6/6 22:48
 * @Description
 */
@Slf4j
public class SyncHotelInfoTaskThread implements Runnable {
    private final String appId;

    private final String key;

    private static final long SAVE_BATCH = 100;

    public SyncHotelInfoTaskThread(String appId, String key) {
        this.appId = appId;
        this.key = key;
    }

    @Override
    public void run() {
        try {
            RedisUtil redisUtil = BeanContext.getApplicationContext().getBean(RedisUtil.class);
            WeHotelTaskService weHotelTaskService = BeanContext.getApplicationContext().getBean(WeHotelTaskService.class);

            while (redisUtil.lGetListSize(RedisKeyConstant.HOTEL_MAIN_WEHOTEL_INNID_LIST) > 0) {
                try {
                    String hotelId = (String) redisUtil.rightPop(RedisKeyConstant.HOTEL_MAIN_WEHOTEL_INNID_LIST);
                    if (null == hotelId) {
                        continue;
                    }

                    GetHotelInfo getHotelInfo = new GetHotelInfo();
                    getHotelInfo.setInnId(hotelId);
                    weHotelTaskService.syncHotelInfoTask(appId, key, getHotelInfo);
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}